Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmGLD160
		Inherits System.Windows.Forms.Form
		Dim OldCICCD As String
		Private Sub WriteToFile()
			Dim SQLInsert As String
			Dim RecCCI As ADODB.Recordset
			If Not IsDate(txtCIDTE.Text) Then
				MsgBox("Invalid Date!")
				Exit Sub
			End If
			If Not IsNumeric(txtCICCR.Text) Or Val(txtCICCR.Text) <= 0 Then
				MsgBox("Invalid Rate!")
				Exit Sub
			End If
			txtCIDTE.Text = VB6.Format(txtCIDTE.Text, "yyyy-mm-dd hh:mm:ss")
			RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtCICCD.Text & "'" & " and day(CIDTE)=" & VB.Day(CDate(txtCIDTE.Text)) & " and month(CIDTE)=" & Month(CDate(txtCIDTE.Text)) & " and year(CIDTE)=" & Year(CDate(txtCIDTE.Text)))
			If RecCCI.EOF Then
				SQLInsert = " INSERT INTO CCI " & "(CICCD,CIDTE,CICCR) " & "VALUES " & "('" & txtCICCD.Text & "', '" & txtCIDTE.Text & "'," & txtCICCR.Text & ")"
			Else
				SQLInsert = "UPDATE CCM SET CICCD = '" & txtCICCD.Text & "', CIDTE ='" & txtCIDTE.Text & "' " & ",CICCR=" & txtCICCR.Text & " WHERE CICCD ='" & OldCICCD & "'"

			End If

			DBconn.Execute(SQLInsert)
			RecCCI.Close()
		End Sub

		Private Sub WriteToScreen()
			Dim cboCMCCD As Object
			Dim TBL As ADODB.Recordset
			If ISADDING Then 'gan rong
				txtCICCD.Text = ""
				txtCMDSC.Text = ""
				txtCICCR.Text = CStr(0)
				txtCIDTE.Text = CStr(Today)
				' txtUMCVF = 0
			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select CCM.*From CCM where  CMCCD ='" & cboCMCCD.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtCICCD.Text = IIf(IsDBNull(TBL.Fields("CMCCD").Value), "", TBL.Fields("CMCCD").Value)
					txtCMDSC.Text = CStr(txtCMDSC.Text = Find("CCM", "CMDSC", " WHERE CMCCD='" & txtCICCD.Text & "'"))
					txtCICCR.Text = IIf(IsDBNull(TBL.Fields("CICCR").Value), "", TBL.Fields("CICCR").Value)
					txtCIDTE.Text = IIf(IsDBNull(TBL.Fields("CIDTE").Value), "", TBL.Fields("CIDTE").Value)
					'    txtUMCVF = IIf(IsNull(TBL!UMCVF), "", TBL!UMCVF)
				Else
					cboCICCD.BoundText = ""
					txtCICCD.Text = ""
					txtCMDSC.Text = ""
					txtCIDTE.Text = CStr(Today)
				End If
				TBL.Close()
			End If
			OldCICCD = txtCICCD.Text
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			Frame1.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub cboCMCCD_Click(ByRef Area As Short)
			If Area = 2 Then
				WriteToScreen()
			End If


		End Sub

		Private Sub cboCICCD_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboCICCD.ClickEvent
			Dim RecCCI As ADODB.Recordset
			If eventArgs.area = 2 Then
				txtCICCD.Text = cboCICCD.BoundText
				Adodc2.RecordSource = "Select * from CCI where CICCD='" & txtCICCD.Text & "' order by CIDTE DESC" 'Currency rate
				Adodc2.Refresh()
				txtCMDSC.Text = cboCICCD.CtlText
				cboCICCD.Visible = False
				If Not ISADDING Then
					RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtCICCD.Text & "' order by CIDTE DESC")
					If Not RecCCI.EOF Then
						txtCICCR.Text = RecCCI.Fields("CICCR").Value
						txtCIDTE.Text = RecCCI.Fields("CIDTE").Value
					Else
						txtCICCR.Text = CStr(0)
					End If
				End If
			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()

			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
			txtCICCD.Text = cboCICCD.BoundText
			WriteToScreen()


		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim cboCMCCD As Object
			Dim SQL As String
			SQL = "DELETE FROM CCM WHERE CMCCD = '" & cboCMCCD.BoundText & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()
			'WriteToScreen (0)

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True


			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click


			WriteToFile()
			Adodc1.Refresh()

			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()


		End Sub

		Private Sub frmGLD160_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "CCM"	'Currency code
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * from CCI" 'Currency rate
			Adodc2.Refresh()
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
			cboCICCD.RowSource = ADOBind_Adodc1.DataSource
			lstCIDTE.RowSource = ADOBind_Adodc2.DataSource

		End Sub

		Private Sub lstCIDTE_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstCIDTE.ClickEvent
			txtCICCD.Text = lstCIDTE.BoundText
			txtCIDTE.Text = lstCIDTE.defText
			txtCMDSC.Text = Find("CCM", "CMDSC", " WHERE CMCCD='" & txtCICCD.Text & "'")
			cboCICCD.Visible = False
			Dim RecCCI As ADODB.Recordset
			RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtCICCD.Text & "'" & " and day(CIDTE)=" & VB.Day(CDate(txtCIDTE.Text)) & " and month(CIDTE)=" & Month(CDate(txtCIDTE.Text)) & " and year(CIDTE)=" & Year(CDate(txtCIDTE.Text)))
			If Not RecCCI.EOF Then
				txtCICCR.Text = RecCCI.Fields("CICCR").Value
			Else
				txtCICCR.Text = CStr(0)
			End If
		End Sub

		Private Sub txtCICCD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtCICCD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			Dim RecCCI As ADODB.Recordset
			If KeyAscii = 13 Then
				Result = Find("CCM", "CMDSC", " WHERE CMCCD='" & txtCICCD.Text & "'")
				Adodc2.RecordSource = "Select * from CCI where CICCD='" & txtCICCD.Text & "' order by CIDTE DESC" 'Currency rate
				Adodc2.Refresh()
				If Result <> "" Then
					txtCMDSC.Text = Result
					RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtCICCD.Text & "' order by CIDTE DESC")
					If Not RecCCI.EOF Then
						txtCICCR.Text = RecCCI.Fields("CICCR").Value
						txtCIDTE.Text = RecCCI.Fields("CIDTE").Value
					Else
						txtCICCR.Text = CStr(0)
						txtCIDTE.Text = ""
					End If
				Else
					cboCICCD.Visible = True
					cboCICCD.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace